package org.social.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.bean.InterceptInfoDTO;
import org.constant.ActionEnum;
import org.constant.ServerTypeEnum;
import org.remote.game.RemoteGameServer;
import org.remote.login.RemoteLoginServer;
import org.social.db.bean.InterceptInfo;
import org.social.db.bean.PlayerMap;
import org.social.db.cache.InterceptInfoCache;
import org.social.db.cache.PlayerMapCache;
import org.utils.RemoteUtil;

import com.google.common.collect.Lists;

/**
 * 拦截管理器
 * Created by fxf on 2017-12-20.
 */
public class InterceptManager {

    private InterceptManager() {
    }



    /**
     * 通知远程
     * @param type
     */
    public static void notifyRemote(ServerTypeEnum type,String serverId){
    	List<InterceptInfo> list = InterceptInfoCache.me().loadAll();
		List<InterceptInfoDTO> notifyList = new ArrayList<>(list.size());
		InterceptInfoDTO dto = null;
		for (InterceptInfo interceptInfo : list) {
			dto = buildInterceptInfoDTO(interceptInfo,ActionEnum.DEFAULT);
			notifyList.add(dto);
		}
//		if (!notifyList.isEmpty()) {
			switch (type) {
			case LOGIN_SERVER:
				RemoteUtil.loginAsyn().noticeIntercept(notifyList);				
				break;
			case GAME_SERVER:
				RemoteUtil.gameAsyn(serverId).noticeIntercept(notifyList);
				break;
			default:
				break;
			}
//		}
    }
    

    /** 添加屏蔽 */
    public static void addInterceptInfo(int type,List<String> targets, Date deadline,String remarks) {
        List<InterceptInfoDTO> noticeList = Lists.newArrayList();
        targets.forEach(target -> {
//        	PlayerMap playerMap = PlayerMapCache.me().getByPid(target);
//        	if (playerMap == null) {
//				return;
//			}
        	InterceptInfo interceptInfo = InterceptInfoCache.me().getByTypeTarget(type, target);
        	ActionEnum action = null;
        	if (interceptInfo!=null) {
        		interceptInfo.setDeadline(deadline);
        		interceptInfo.update();
        		action = ActionEnum.UPDATE;
			}else {
				interceptInfo = new InterceptInfo(0, type, target, deadline, remarks);
	        	interceptInfo.insert();
	        	action = ActionEnum.ADD;
			}
        	noticeList.add(buildInterceptInfoDTO(interceptInfo,action));
        });
        // 通知
        notifyAllServerIntercept(noticeList);
    }

    /** 删除屏蔽 */
    public static void removeInterceptInfo(int...ids) {
    	List<InterceptInfoDTO> noticeList = new ArrayList<>(ids.length);
    	for (int id : ids) {
    		InterceptInfo interceptInfo = InterceptInfoCache.me().getById(id);
    		if (interceptInfo == null) {
				continue;
			}
        	interceptInfo.delete();
    		noticeList.add(buildInterceptInfoDTO(interceptInfo,ActionEnum.REMOVE));
		}
    	// 通知
    	notifyAllServerIntercept(noticeList);
    }

    
    
    public static void notifyAllServerIntercept(List<InterceptInfoDTO> list) {
        RemoteGameServer remoteGameServer = RemoteUtil.gameTopic();
        remoteGameServer.noticeIntercept(list);
        
        RemoteLoginServer remoteLoginServer = RemoteUtil.loginAsyn();
        remoteLoginServer.noticeIntercept(list);
    }


    private static InterceptInfoDTO buildInterceptInfoDTO(InterceptInfo info,ActionEnum action) {
        return new InterceptInfoDTO(info.getType(), info.getTarget(), info.getDeadline(), action);
    }

}